/**
 * Created by cxj on 2016/2/22.
 */


var bootbox_global;
var grid;

define([
    "jquery",
    "IO",
    "bootbox"
], function($,IO,bootbox) {
    $(function () {

        bootbox_global = bootbox;



        //每列数据列性
        var columns = [

            { title: '等级', dataIndex: 'type',dataSource: function (data, datarow, gridobj, current_column) {
                var parent_id = data[datarow].parent_id;
                if(parent_id==-1)
                    return  "<span style='color: #00a2d4;text-weight:bold;'>一级菜单</span>";

                return  "";

            }},
            //{ title: '角色id', dataIndex: 'menu_id',},

            { title: '菜单名', dataIndex: 'menu_name'},
            { title: 'url', dataIndex: 'url'},
            { title: '创建时间', dataIndex: 'create_time',},
            { title: '修改时间', dataIndex: 'update_time',},
            {title : '操作',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                var value = "checked='checked'";

                if(data[datarow].is_select == 0)
                    value = "";

                var  html =  "<input "+value+" type=\"checkbox\" id='checkbox"+data[datarow].menu_id+"'>"




                return html;
            }},


        ];

        //数组初始化

        function dataInit() {
            grid = new Grid("/role/getmenuinfo", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 2000);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            //var param="mobile="+$("#mobile").val();

            //return param;

            return "role_id="+role_id;
        }
        dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });


        //取消
        $("#btn_cancel").bind("click",function() {

            jump("/role/rolelist");
        });


        //提交
        $("#btn_confrim").bind("click",function() {

            var datalist = grid.date_list;

            var select_list = "";
            var unselect_list = "";

            //alert(datalist.length);

            for(var i=0;i<datalist.length;i++)
            {
                var obj = datalist[i];

                var check_box_id = "checkbox"+obj.menu_id;

                var checkobj = document.getElementById(check_box_id);

                if(checkobj.checked)
                {
                    if(select_list == "")
                        select_list = obj.menu_id;
                    else
                        select_list = select_list+","+obj.menu_id;

                }
                else
                {
                    if(unselect_list == "")
                        unselect_list = obj.menu_id;
                    else
                        unselect_list = unselect_list+","+obj.menu_id;

                }
            }

            //发送请求
            send_request_direct("/role/setmenuinfo", "role_id="+role_id+"&select_list="+select_list+"&unselect_list="+unselect_list, function process_data(data) {


                //操作成功
                AlertSuccess(bootbox, "操作成功", function () {

                    window.location = '/role/rolelist';


                });


            }, function process_fail(data) {

                //操作失败
                AlertError(bootbox, data.msg);


            });


        });
    });


});